package org.sky.service.serviceImpl;

import org.apache.log4j.Logger;
import org.sky.base.DaoSupport;
import org.sky.model.FileUtil;
import org.sky.service.FileUtilService;
import org.sky.service.PersonService;
import org.sky.vo.FileVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class FileUtilServiceImpl extends DaoSupport<FileUtil> implements FileUtilService {

    private final static Logger logger = Logger.getLogger(FileUtilServiceImpl.class);
    private PersonService personService;


    @Override
    public boolean uploadFile(FileVO fileVO) throws Exception {

        FileUtil fileUtil = new FileUtil();
        fileUtil.setAuthor(personService.findPersonByUserName(fileVO.getAuthor()));
        fileUtil.setDescription(fileVO.getDescription());
        fileUtil.setFileDir(fileVO.getUploadDir());
        fileUtil.setFileName(fileVO.getUploadFileName());
        fileUtil.setDate(fileVO.getDate());
        fileUtil.setContentType(fileVO.getUploadContentType());
        fileUtil.setSize(fileVO.getSize());

        try {
            this.save(fileUtil);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e);
            return false;
        }

    }

    /**
     * 对文件进行备份
     */
    @Override
    public void backupFile(File file, String fileName, String savePathDir, String username) {

        // 保存文件真实路径
        File fileSaved = new File(savePathDir);
        if (!fileSaved.exists())
            fileSaved.mkdirs();// 创建真实路径

        // 保存文件
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            byte[] buffer = new byte[1024];
            fileOutputStream = new FileOutputStream(new File(savePathDir,
                    fileName));
            int len = 0;
            while ((len = fileInputStream.read(buffer)) > 0) {
                fileOutputStream.write(buffer, 0, len);
            }
            fileInputStream.close();
            fileOutputStream.close();
            logger.info(username + "上传了文件: " + fileName);
        } catch (IOException e) {
            logger.error("Can't backup the file", e);
        }
    }

    @Override
    public FileUtil findByFileName(String fileName) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("fileName", fileName);
        return this.findSpecialObject(FileUtil.class, params);
    }

    @Resource(name = "personServiceImpl")
    public void setPerson(PersonService personService) {
        this.personService = personService;
    }
}
